package com.clusterswisdom.horn.api.tmall.action.scitem;

import java.util.Iterator;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.clusterswisdom.horn.api.ioc.HornAction;
import com.clusterswisdom.horn.api.ioc.HornResponse;
import com.clusterswisdom.horn.api.ioc.api.ApiConfig;
import com.clusterswisdom.horn.api.tmall.TmallInstance;
import com.clusterswisdom.horn.api.tmall.action.AbstractTmallAction;
import com.clusterswisdom.horn.api.tmall.action.scitem.model.ScItem;
import com.clusterswisdom.horn.api.tmall.action.scitem.model.Store;
import com.clusterswisdom.horn.api.tmall.action.scitem.model.StoreInfo;
import com.taobao.api.response.InventoryInitialResponse;
/**
 * 初始化区域商品库存（只能初始化一次）
 * @author Jin_tian
 *
 */
public class InitialStoryAction extends AbstractTmallAction implements HornAction{

	public static final String PARAM_ITEMS = "items";
	
	public static final String PARAM_STORE = "store";
	
	public static final String PARAM_SCITEMID = "cai";
	
	public static final String PARAM_QUANTITY = "tmallStock";
	
	@Override
	public HornResponse execute(ApiConfig config, JSONObject param) throws Exception {
		StoreInfo inputInfo = getInputStoreInfo(param);
		String failInfo = initInventory(inputInfo);
		if (failInfo.length() == 0) {
			return new HornResponse(true, null);
			
		} else {
			return new HornResponse(false, failInfo);
		}
	}
	
	private String initInventory(StoreInfo inputInfo) throws Exception {
		StringBuilder failInfo = new StringBuilder(100);
		
		Iterator<Store> storeItreator = inputInfo.values().iterator();
		while (storeItreator.hasNext()) {
			Store store = storeItreator.next();
			
			JSONObject request=new JSONObject();
			request.put("store_code", store.getCode());
			request.put("items", getStoreItemParam(store));
			
			InventoryInitialResponse response = (InventoryInitialResponse)TmallInstance.getInstance().executeRequest(request, "scitem","inventoryinitial");
			if (!response.isSuccess()) {
				failInfo.append(getUnsuccessResponse(response)).append(";");
			}
		}
		
		return failInfo.toString();
	}
	
	protected StoreInfo getInputStoreInfo(JSONObject param) {
		StoreInfo storeInfo = new StoreInfo();
		for (Object object : param.getJSONArray(PARAM_ITEMS)) {
			JSONObject itemObj = (JSONObject)object;
			String storeId = itemObj.getString(PARAM_STORE);
			String scItemId = itemObj.getString(PARAM_SCITEMID);
			Long quantity = itemObj.getLong(PARAM_QUANTITY);
				
			storeInfo.addStore(storeId, scItemId, quantity);
		}
		
		return storeInfo;
	}
	
	private String getStoreItemParam(Store inputStore) {
		JSONArray itemArray = new JSONArray();
		
		Iterator<ScItem> itemIterator = inputStore.values().iterator();
		while (itemIterator.hasNext()) {
			ScItem inpuItem = itemIterator.next();
			
			JSONObject item = new JSONObject();
			item.put("scItemId", "0");
			item.put("scItemCode", inpuItem.getItemId());
			item.put("inventoryType", "1");
			item.put("quantity", Long.toString(inpuItem.getQuantity()));
			
			itemArray.add(item);
		}
		
		return JSON.toJSONString(itemArray);
	}
	
}